package org.linlinjava.litemall.wx.service;

import org.linlinjava.litemall.core.systemConfig.SystemConfig;
import org.linlinjava.litemall.db.dao.*;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.wx.util.CreateSn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 抽奖服务
 *
 * @author zhy
 * @date 2019-07-03 11:07
 **/
@Service
public class LotteryService {
	@Resource
	private LitemallUserLotteryMapper litemallUserLotteryMapper;
    @Resource
    private LitemallAwardStatisticsMapper statisticsMapper;
    @Resource
    private LitemallUserMapper litemallUserMapper;
    @Resource
    private LitemallAwardPlanMapper litemallAwardPlanMapper;
    @Resource
    private LitemallWalletRecordMapper litemallWalletRecordMapper;
	@Resource
	private LitemallAwardMapper litemallAwardMapper;
	@Resource
	private CreateSn createSn;

    public LitemallUserLottery confirmHasAward(Integer userId){
	    LitemallUserLotteryExample example = new LitemallUserLotteryExample();
	    example.or().andAwardIdIsNotNull().andUserIdEqualTo(userId).andCreateTimeBetween(LocalDateTime.now().minusDays(15),LocalDateTime.now());
	    return litemallUserLotteryMapper.selectOneByExample(example);
    }

    public int updateLotteryRecord(LitemallUserLottery litemallUserLottery){
    	return litemallUserLotteryMapper.updateByPrimaryKeySelective(litemallUserLottery);
    }

	public long getUserLotteryNum(Integer userId){
		LitemallUserLotteryExample  example = new LitemallUserLotteryExample();
		example.or().andUserIdEqualTo(userId).andDrawTimeEqualTo(LocalDate.now());
		return litemallUserLotteryMapper.countByExample(example);
	}

	public LitemallAwardStatistics getNum(){
		LitemallAwardStatisticsExample example = new LitemallAwardStatisticsExample();
		return statisticsMapper.selectOneByExample(example);
	}

	public int reduce(Integer userId){
		return litemallUserMapper.reductById(10,userId);
	}

	public int insertRecord(Integer userId){
		LitemallUserLottery litemallUserLottery = new LitemallUserLottery();
		litemallUserLottery.setUserId(userId);
		litemallUserLottery.setDrawTime(LocalDate.now());
		litemallUserLotteryMapper.insertSelective(litemallUserLottery);
		return litemallUserLottery.getId();
	}

	public List<LitemallAwardPlan> getAwardPlan(){
		LitemallAwardPlanExample example = new LitemallAwardPlanExample();
		example.or().andActiveDateEqualTo(LocalDate.now()).andDeletedEqualTo(false);
		return litemallAwardPlanMapper.selectByExample(example);
    }

    public LitemallAwardPlan getOnePlan(){
	    LitemallAwardPlanExample example = new LitemallAwardPlanExample();
        example.or().andDeletedEqualTo(false).andActiveDateEqualTo(LocalDate.now()).andLeftNumGreaterThan(0);
        return litemallAwardPlanMapper.selectOneByExample(example);
    }

    public long countAward(){
	    LitemallAwardPlanExample example = new LitemallAwardPlanExample();
	    example.or().andDeletedEqualTo(false).andActiveDateEqualTo(LocalDate.now()).andLeftNumGreaterThan(0);
	    return litemallAwardPlanMapper.countByExample(example);
    }

    public List<LitemallAward> getAwardList(){
	    LitemallAwardExample example = new LitemallAwardExample();
	    example.or().andStatusEqualTo(false);
	    example.orderBy(LitemallAward.Column.sort.asc());
	    LitemallAward.Column[] column = new LitemallAward.Column[]{LitemallAward.Column.id,LitemallAward.Column.awardName,LitemallAward.Column.url,LitemallAward.Column.sort};
	    return litemallAwardMapper.selectByExampleSelective(example,column);
    }

    public int reduceNum(Integer planId){
    	return litemallAwardPlanMapper.reduceNum(1,planId);
    }

    @Async
    public void insertWallet(Integer userId){
    	LitemallWalletRecord litemallWalletRecord = new LitemallWalletRecord();
	    String recordSn = createSn.createSn();
	    litemallWalletRecord.setUserId(userId);
	    litemallWalletRecord.setType((byte)2);
	    litemallWalletRecord.setRecordSn(recordSn);
	    litemallWalletRecord.setModifyIntegral(-SystemConfig.getLotteryIntegral());
	    litemallWalletRecord.setModifyReason("抽奖");
	    litemallWalletRecord.setModifyTime(LocalDateTime.now());
	    litemallWalletRecord.setPayTime(LocalDateTime.now());
	    litemallWalletRecord.setPayId(recordSn);
	    litemallWalletRecordMapper.insertSelective(litemallWalletRecord);
    }

    public List<LitemallUserLottery> getAwardUser(){
        LitemallUserLotteryExample example = new LitemallUserLotteryExample();
        example.or().andAwardIdIsNotNull();
	    example.orderBy(LitemallUserLottery.Column.drawTime.desc());
	    LitemallUserLottery.Column[] columns = new LitemallUserLottery.Column[]{LitemallUserLottery.Column.awardName,LitemallUserLottery.Column.userName};
	    return litemallUserLotteryMapper.selectByExampleSelective(example,columns);
    }
}
